
var app = getApp();

Page({
  data: {
    showDevices: false,
    selectedDevice: {},
    totalDevices: {},
    showLoading: false,
    timeColumn: [],
    otherColumns: [],

    showStartDate: false,
    showEndDate: false,

    buttonDaysIndex: 0,
    searchType: 0,

    pageIndex: 0,
    cursors: [],

  },
  onLoad: function () {
    //检测是否登录
    if (!app.globalData.userInfo) {
      wx.navigateTo({
        url: '/pages/login/login',
      });
    }
    this.setData({
      selectedDevice: app.globalData.selectedDevice,
      totalDevices: app.globalData.totalDevices,
      minDate: new Date(this.getDateStr(undefined, -30)).getTime(),
      maxDate: new Date().getTime()
    });

    this.searchOneDay();
  },

  getDeviceHistoryData(pageIndex, cursor) {
    //按分钟查询
    if (this.data.searchType == 0) {
      this.getDeviceHistoryDataPerMinute(pageIndex, cursor);
    }
    else {
      this.getDeviceHistoryDataPerDay(pageIndex);
    }
  },
  getDeviceHistoryDataPerMinute(pageIndex, cursor) {
    this.setData({ showLoading: true });
    var startDate = this.data.startDate;
    var endDate = this.data.endDate;
    var currentPage = this;
    var dataOption = {
      deviceId: this.data.selectedDevice.deviceId,
      deviceTypeId: this.data.selectedDevice.deviceTypeId,
      startDate: startDate,
      endDate: endDate
    };
    if (cursor) {
      dataOption.cursor = cursor;
    }
    wx.request({
      url: app.globalData.baseUrl + '/api/services/app/OneNetApiService/GetDeviceHistoryValuesPerMinute',
      data: dataOption,
      header: {
        'content-type': 'application/json', // 默认值
        'Authorization': 'Bearer ' + app.globalData.token
      },
      success(res) {
        var result = res.data.result;
        var cursors = currentPage.data.cursors;
        if (pageIndex > (cursors.length - 1)) {
          cursors.push(result.cursor);
          currentPage.setData({
            cursors: cursors,
          });
        }
        currentPage.setData({
          pageIndex: pageIndex
        });
        var datasource = result.rows[0].columns;
        currentPage.setData({
          timeColumn: datasource[0].datapoints,
          otherColumns: datasource
        });
      },
      fail(res) {
        console.log(res);
      },
      complete(res) {
        currentPage.setData({
          showLoading: false
        });
      }
    });
  },
  getDeviceHistoryDataPerDay(pageIndex) {
    this.setData({ showLoading: true });
    var startDate = this.data.startDate;
    var endDate = this.data.endDate;
    var currentPage = this;
    var dataOption = {
      deviceId: this.data.selectedDevice.deviceId,
      deviceTypeId: this.data.selectedDevice.deviceTypeId,
      startDate: startDate,
      endDate: endDate,
      pageIndex: pageIndex
    };
    wx.request({
      url: app.globalData.baseUrl + '/api/services/app/OneNetApiService/GetDeviceHistoryValuesPerDay',
      data: dataOption,
      header: {
        'content-type': 'application/json', // 默认值
        'Authorization': 'Bearer ' + app.globalData.token
      },
      success(res) {
        var result = res.data.result;
        currentPage.setData({
          pageIndex: pageIndex
        });
        var datasource = result.rows[0].columns;
        currentPage.setData({
          timeColumn: datasource[0].datapoints,
          otherColumns: datasource
        });
      },
      fail(res) {
        console.log(res);
      },
      complete(res) {
        currentPage.setData({
          showLoading: false
        });
      }
    });
  },

  searchByDay() {
    this.setData({ searchType: 1 });
    this.getDeviceHistoryData(0);
  },
  searchByMinute() {
    this.setData({ searchType: 0 });
    this.getDeviceHistoryData(0);
  },
  showDeviceList() {
    this.setData({ showDevices: true });
  },
  closeDeviceList() {
    this.setData({ showDevices: false });
  },
  changeSelectedDevice(e) {
    var selected = e.currentTarget.dataset["item"];
    this.setData({
      selectedDevice: selected,
      showDevices: false
    });
    this.getDeviceHistoryData(0);
  },
  lastPage() {
    var pageIndex = this.data.pageIndex;
    var cursor = this.data.cursors[pageIndex - 2];

    if (cursor) {
      this.getDeviceHistoryData(pageIndex - 1, cursor);
    } else {
      this.getDeviceHistoryData(pageIndex - 1);
    }
  },
  nextPage(e) {
    var pageIndex = this.data.pageIndex;
    var cursors = this.data.cursors;
    var currentCursor = cursors[pageIndex];
    this.getDeviceHistoryData(pageIndex + 1, currentCursor);
  },

  onShowStartDate(event) {
    this.setData({ showStartDate: true });
  },
  onCloseStartDate() {
    this.setData({ showStartDate: false });
  },
  onConfirmStartDate(event) {
    this.setData({
      showStartDate: false,
      startDate: this.formatDate(event.detail),
    });
  },

  onShowEndDate(event) {
    this.setData({ showEndDate: true });
  },
  onCloseEndDate() {
    this.setData({ showEndDate: false });
  },
  onConfirmEndDate(event) {
    this.setData({
      showEndDate: false,
      endDate: this.formatDate(event.detail),
    });
  },

  formatDate(date) {
    date = new Date(date);
    return date.toLocaleDateString();
  },
  searchOneDay(event) {
    this.setData({
      startDate: this.getDateStr(undefined, 0),
      endDate: this.getDateStr(undefined, 1),
      cursors: [],
      pageIndex: 0,
      buttonDaysIndex: 0
    });
    this.getDeviceHistoryData(0);
  },
  searchThreeDays(event) {
    this.setData({
      startDate: this.getDateStr(undefined, -2),
      endDate: this.getDateStr(undefined, 1),
      cursors: [],
      pageIndex: 0,
      buttonDaysIndex: 1
    });
    this.getDeviceHistoryData(0);
  },
  searchFiveDays(event) {
    this.setData({
      startDate: this.getDateStr(undefined, -4),
      endDate: this.getDateStr(undefined, 1),
      cursors: [],
      pageIndex: 0,
      buttonDaysIndex: 2
    });
    this.getDeviceHistoryData(0);
  },
  getDateStr(today, addDayCount) {
    var dd;
    if (today) {
      dd = new Date(today);
    } else {
      dd = new Date();
    }
    dd.setDate(dd.getDate() + addDayCount);//获取AddDayCount天后的日期 
    var y = dd.getFullYear();
    var m = dd.getMonth() + 1;//获取当前月份的日期 
    var d = dd.getDate();
    if (m < 10) {
      m = '0' + m;
    };
    if (d < 10) {
      d = '0' + d;
    };
    return y + "-" + m + "-" + d;
  }
})